var ctx=window.daijia.ctx;


$(document).ready(function(){
	
	$("#startTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true });
	$("#endTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true });
	
	$('#companyIds').multiselect({
		enableFiltering: true,
	    includeSelectAllOption: true,
	    selectAllText: '全部',
	    selectAllJustVisible: true,
	    maxHeight:300,
	    buttonWidth: '100%',
	    nonSelectedText: '请选择所属公司',
	    nSelectedText: ' - 已选择',
	    numberDisplayed: 4,
	    allSelectedText: '已选择全部'
	});
	
	//添加优惠券
	$("#add_coupon").click(function(){
		var element="<div class='form-group coupon_div'>"+
		                    "<label class='col-sm-3 control-label'></label>"+
		                    "<div class='col-sm-3'>"+
		                    	$("#first_coupon_div select").clone().prop("outerHTML")+
		                    "</div>"+
		                    "<label style='float:left;' class='control-label'><em>*</em>数量：</label>"+
		                    "<div class='col-sm-1'>"+
		                    	"<input class='form-control couponAmount' type='number' min='1' value='1'/>"+
		                    "</div>"+
		                    "<div class='col-sm-1'>"+
		                    	"<button type='button' class='btn btn-default delete_coupon_rule'>"+
								  "<span class='glyphicon glyphicon-remove-sign'></span> 删除"+
								"</button>"+
		                    "</div>"+
	                    "</div>";
		$("#first_coupon_div").after(element);
	});
	
	//删除优惠券
	$(document).on("click",".delete_coupon_rule",function(){
		$(this).parent().parent().remove();
	});
	
	
	
	
	
	
	$("#submit_button").click(function(){
		
		var flag=false;
		var currentSelect;
		$(".couponRule_select").each(function(index,element){
			if($(element).val()==""){
				flag=true;
				currentSelect=element;
				return false;
			}
		})
		if(flag){
			$(currentSelect).focus();
			$.scojs_message('请选择一张优惠券！', $.scojs_message.TYPE_ERROR);
		    return false;
		}
		
		var couponRuleId=[];
		var amount=[];
		var eachLimit=[];
		var numerAll=[];
		$(".coupon_div").each(function(index,element){
			couponRuleId.push($(element).find(".couponRule_select").val());
			amount.push($(element).find(".couponAmount").val());
			eachLimit.push($(element).find(".couponRule_select option:selected").attr("eachLimit"));
			numerAll.push($(element).find(".couponRule_select option:selected").attr("numerAll"));
		});
		$("#couponRuleId").val(couponRuleId);
		$("#amount").val(amount);
		
		var isRedo = false;
		var s = couponRuleId.join(",")+","; 
		for(var i=0;i<couponRuleId.length;i++) { 
			if(s.replace(couponRuleId[i]+",","").indexOf(couponRuleId[i]+",")>-1) { 
				isRedo = true;
			}
		}
		if(isRedo){
			$.scojs_message("优惠券重复，请重新选择！", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		
		for (var i = 0; i < amount.length; i++) {
			if(amount[i]>10){
				$.scojs_message("优惠券输入数量不能大于10", $.scojs_message.TYPE_ERROR);
				$($(".coupon_div")[i]).find(".couponAmount").focus();
				return false;
			}
			if(eachLimit[i]!=0){
				if(parseInt(amount[i])>parseInt(eachLimit[i])){
					$.scojs_message("该优惠券输入数量不能大于每人限领数量", $.scojs_message.TYPE_ERROR);
					$($(".coupon_div")[i]).find(".couponAmount").focus();
					return false;
				}
			}
			if(parseInt(amount[i])>parseInt(numerAll[i])){
				$.scojs_message("该优惠券输入数量不能大于优惠券规则总数量", $.scojs_message.TYPE_ERROR);
				$($(".coupon_div")[i]).find(".couponAmount").focus();
				return false;
			}
			
		}
		
		
		
		
		
		if($("input[name=messageType]:checked").length==0){
			$.scojs_message("请选择通知方式", $.scojs_message.TYPE_ERROR);
			return false;
		}
		if(window.confirm('确认条件并赠送吗？')){
			$("#submit_button").attr("disabled",true);
			$.post(ctx+'/activity/giftCoupon',$("#submit_form").serialize(),function(data){
				if(data.success){
					$.scojs_message("赠送成功", $.scojs_message.TYPE_OK);
					window.location=ctx+"/activity/list?event="+$("#activityEvent").val()+"&type="+$("#activityType").val()+"";
				}
				else{
					$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
					$("#submit_button").attr("disabled",false);
				}
			});
		}
		
		return false;
	});
	
});
